package com.cgw.room.socket;


import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by melo on 2017/9/21.
 */

public class HeartBeatTimer {

    private Timer timer;
    private Timer timer2;
    private TimerTask task;
    private TimerTask task2;
    private OnFirstScheduleListener mListener;
    private OnSecondScheduleListener mListener2;

    private boolean mRunning = false;
    public HeartBeatTimer() {
        timer = new Timer();
        timer2 = new Timer();
    }

    public void startTimer(long delay, long period) {
        task = new TimerTask() {
            @Override
            public void run() {
                if (mListener != null) {
                    mListener.onSchedule();
                }
            }
        };
        task2 = new TimerTask() {
            @Override
            public void run() {
                if (mListener2 != null) {
                    mListener2.onSchedule();
                }
            }
        };
        timer.schedule(task, delay, period);
        timer2.schedule(task2, delay, period * 4);
        mRunning = true;
    }

    public void exit() {
        if (timer != null) {
            timer.cancel();
        }
        if (timer2 != null) {
            timer2.cancel();
        }
        if (task != null) {
            task.cancel();
        }
        mRunning = false;
    }

    public boolean isRunning(){
        return mRunning;
    }

    public interface OnFirstScheduleListener {
        void onSchedule();
    }
    public interface OnSecondScheduleListener {
        void onSchedule();
    }

    public void setOnFirstScheduleListener(OnFirstScheduleListener listener) {
        this.mListener = listener;
    }
    public void setOnSecondScheduleListener(OnSecondScheduleListener listener) {
        this.mListener2 = listener;
    }
}

